package com.dtw.ditouwang.ui.coin

import android.support.v7.widget.RecyclerView
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.dtw.ditouwang.R
import com.dtw.ditouwang.beans.OrderListBean
import com.dtw.ditouwang.models.WebApi
import com.dtw.ditouwang.ui.OnItemClickListener
import kotlinx.android.synthetic.main.item_order.view.*
import java.text.DecimalFormat

class OrderAdapter(var orderList: ArrayList<OrderListBean.DataBean>) : RecyclerView.Adapter<OrderAdapter.Holder>() {
    var onItemClickListener: OnItemClickListener? = null
    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): Holder {
        return Holder(LayoutInflater.from(p0.context).inflate(R.layout.item_order, p0, false))
    }

    override fun getItemCount(): Int {
        return orderList.size
    }

    override fun onBindViewHolder(holder: Holder, position: Int) {
        holder.itemView.orderNumText.text = "订单编号：${orderList[position].order_number}"
        holder.itemView.orderStateText.text = orderList[position].status
        Glide.with(holder.itemView.productImage).load(WebApi.BaseUrl + orderList[position].cover).into(holder.itemView.productImage)
        holder.itemView.orderNumText.text = orderList[position].shop_name
        holder.itemView.timeText.text = orderList[position].time
        holder.itemView.spendText.text = "已使用${DecimalFormat("#.#####").format(orderList[position].gold_total)}金币"
        holder.itemView.orderTransport.text = "物流信息：${orderList[position].express_type}  ${orderList[position].express_order}"
        if(TextUtils.isEmpty(orderList[position].express_type)&&TextUtils.isEmpty(orderList[position].express_order)){
            holder.itemView.orderTransport.text = "物流信息：暂无"
        }
        if (orderList[position].status_id == "3") {
            holder.itemView.confirmTransportText.visibility = View.GONE
        }
    }

    inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        init {
            itemView.confirmTransportText.setOnClickListener{onItemClickListener?.onItemClick(0, adapterPosition, 0) }
        }
    }
}